package main

import (
	"github.com/gin-gonic/gin"
	"log"
	"time"
)

func Test(c *gin.Context) {
	log.Print("111111")

	//c.Abort()
	//return
	c.Next()
	log.Print("222222")
}
func Logger() gin.HandlerFunc {
	return func(c *gin.Context) {
		t := time.Now()

		// Set example variable
		c.Set("example", "middlerware test 12345")

		// before request

		c.Next()

		// after request
		latency := time.Since(t)
		log.Print(latency)

		// access the status we are sending
		status := c.Writer.Status()
		log.Println(status)
	}
}

func main() {
	r := gin.New()
	r.Use(Logger())
	r.Use(Test)

	r.GET("/test", func(c *gin.Context) {
		example := c.MustGet("example").(string)

		// it would print: "12345"
		log.Println(example)
		c.Writer.WriteString("helloworld")
	})

	// Listen and serve on 0.0.0.0:9999
	r.Run(":9999")
}

/*
output:

2023/08/23 15:21:53 111111
2023/08/23 15:21:53 middlerware test 12345
2023/08/23 15:21:53 222222
2023/08/23 15:21:53 50.577µs
2023/08/23 15:21:53 200

*/
